<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package        CodeIgniter
 * @author        ExpressionEngine Dev Team
 * @copyright    Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license        http://codeigniter.com/user_guide/license.html
 * @link        http://codeigniter.com
 * @since        Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Model Class
 *
 * @package        CodeIgniter
 * @subpackage    Libraries
 * @category    Libraries
 * @author        ExpressionEngine Dev Team
 * @link        http://codeigniter.com/user_guide/libraries/config.html
 */
class CI_Model
{


    /**
     * Constructor
     *
     * @access public
     */
    function __construct()
    {
        $this->load->database();
        log_message('debug', "Model Class Initialized");
    }

    /**
     * 基本更新操作
     * @return mixed基本
     */
    public function update($arr)
    {
        return $this->db->update($this->getTableName(), $arr, array('id' => $arr['id']));
    }

    public function insert($arr)
    {
        return $this->db->insert($this->getTableName(), $arr);
    }

    public function load()
    {
        return $this->db->query("select * from " . $this->getTableName() . " where id=" . $this->getId())->row();
    }

    public function delete()
    {
        return $this->db->delete($this->getTableName(), array("id"=> $this->getId()));
    }

    public function getInsertId()
    {
        return $this->db->insert_id();
    }

    /**
     * __get
     *
     * Allows models to access CI's loaded classes using the same
     * syntax as controllers.
     *
     * @access private
     */
    function __get($key)
    {
        $CI =& get_instance();
        return $CI->$key;
    }


    public $id;
    private $_tableName;
    private $_query;


    public function setId($id)
    {
        $this->id = $id;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getTableName()
    {
        return $this->_tableName;
    }

    public function setTableName($tableName)
    {
        $this->_tableName = $tableName;
    }

    protected function mappingObject()
    {
        $class = new ReflectionClass($this);
        $result = $this->getQuery()->result();

        foreach ($class->getProperties() as $key => $value) {
            $propertyName = $value->name;
            $this->$propertyName = $result[0]->$propertyName;


        }
        return $this;
    }

    protected function mappingList()
    {
        $class = new ReflectionClass($this);
        $result = $this->getQuery()->result();
        $resultList = array();
        foreach ($result as $resultKey => $resultValue) {
            foreach ($class->getProperties() as $key => $value) {
                $propertyName = $value->name;
                $this->$propertyName = $resultValue->$propertyName;
            }
            array_push($resultList, clone  $this);
        }
        return $resultList;
    }

    public function setQuery($query)
    {
        $this->_query = $query;
    }

    public function getQuery()
    {
        return $this->_query;
    }


}
// END Model Class

/* End of file Model.php */
/* Location: ./system/core/Model.php */